package cn.ibizlab.odoo.core.odoo_resource.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iresource_resource;
import cn.ibizlab.odoo.core.odoo_resource.domain.Resource_resource;
import cn.ibizlab.odoo.core.odoo_resource.filter.Resource_resourceSearchContext;
import cn.ibizlab.odoo.core.odoo_resource.service.IResource_resourceService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[资源] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Resource_resourceServiceImpl implements IResource_resourceService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iresource_resourceClientService resource_resourceClientService;

    @Override
    public void updateBatch(List<Resource_resource> resource_resources, int batchSize) {

        //resource_resourceClientService.updateBatch(resource_resources,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //resource_resourceClientService.removeBatch(resource_resources) ;

    }

    @Override
    public boolean update(Resource_resource resource_resource) {
        boolean bOk = false ;
        Iresource_resource clientModel = this.convert2Model(resource_resource,null);
        resource_resourceClientService.update(clientModel) ;
        this.convert2Domain(clientModel,resource_resource) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Resource_resource get(Integer id) {
        Iresource_resource clientModel = resource_resourceClientService.createModel();
        clientModel.setId(id);
        resource_resourceClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Resource_resource get(Resource_resource resource_resource) {
        Resource_resource retresource_resource = this.get(resource_resource.getId()) ;
    	BeanCopier copier=BeanCopier.create(retresource_resource.getClass(), resource_resource.getClass(), false);
        copier.copy(retresource_resource, resource_resource, null);
        return resource_resource ;
    }

    @Override
    public boolean create(Resource_resource resource_resource) {
        boolean bOk = false ;
        Iresource_resource clientModel = this.convert2Model(resource_resource,null);
        resource_resourceClientService.create(clientModel) ;
        this.convert2Domain(clientModel,resource_resource);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void createBatch(List<Resource_resource> resource_resources, int batchSize) {

                        //resource_resourceClientService.createBatch(resource_resources,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iresource_resource clientModel = resource_resourceClientService.createModel();
        clientModel.setId(id);
        resource_resourceClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Resource_resource> searchDefault(Resource_resourceSearchContext context) {
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据资源工作时间查询
     */
    @Override
    public Page<Resource_resource> selectByOdoo_calendar(Integer id){
        Resource_resourceSearchContext context = new Resource_resourceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据公司查询
     */
    @Override
    public Page<Resource_resource> selectByOdoo_company(Integer id){
        Resource_resourceSearchContext context = new Resource_resourceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Resource_resource> selectByOdoo_create(Integer id){
        Resource_resourceSearchContext context = new Resource_resourceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Resource_resource> selectByOdoo_user(Integer id){
        Resource_resourceSearchContext context = new Resource_resourceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Resource_resource> selectByOdoo_write(Integer id){
        Resource_resourceSearchContext context = new Resource_resourceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Resource_resource> list = new ArrayList<Resource_resource>();
        Page<cn.ibizlab.odoo.core.client.model.Iresource_resource> clientModelList = resource_resourceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iresource_resource clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Resource_resource>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iresource_resource convert2Model(Resource_resource domain , Iresource_resource model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = resource_resourceClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getTime_efficiencyDirtyFlag())
            model.setTime_efficiency(domain.getTime_efficiency());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getResource_typeDirtyFlag())
            model.setResource_type(domain.getResource_type());
         if(domain.getTzDirtyFlag())
            model.setTz(domain.getTz());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getCalendar_id_textDirtyFlag())
            model.setCalendar_id_text(domain.getCalendar_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCompany_id_textDirtyFlag())
            model.setCompany_id_text(domain.getCompany_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCalendar_idDirtyFlag())
            model.setCalendar_id(domain.getCalendar_id());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Resource_resource convert2Domain( Iresource_resource model ,Resource_resource domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Resource_resource();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getTime_efficiencyDirtyFlag())
            domain.setTime_efficiency(model.getTime_efficiency());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getResource_typeDirtyFlag())
            domain.setResource_type(model.getResource_type());
        if(model.getTzDirtyFlag())
            domain.setTz(model.getTz());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getCalendar_id_textDirtyFlag())
            domain.setCalendar_id_text(model.getCalendar_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCompany_id_textDirtyFlag())
            domain.setCompany_id_text(model.getCompany_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCalendar_idDirtyFlag())
            domain.setCalendar_id(model.getCalendar_id());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
